// File to download ATUS files.
//
// Jeff Shrader
// Creation date: 2017-09-03
// Time-stamp: "2018-02-04 17:37:03 jgs"

// Directories
local work "/DIRECTORY"

// Data directory
cd "`work'"
local ext_dir = "`work'/data"

// Flags for what years you want to download
// If you download a new combined dataset, update this
local start_year "03"
local end_year "16"
// To download the wellbeing module, update this
local wb_start "10"
local wb_end "13"
// Leave module
local lv_start "2011"
// Eating and Health
local health_years "2006 2007 2008 2014 2015"

// Get the files
// Main ATUS files
local file_list "resp rost act sum who cps wgts"
confirmdir "`ext_dir'/atus`start_year'`end_year'"
if r(confirmdir) == "170" {
   mkdir "`ext_dir'/atus`start_year'`end_year'"
}
cd "`ext_dir'/atus`start_year'`end_year'"
foreach i of local file_list {
   local filename "atus`i'_`start_year'`end_year'.zip"
   di "`filename'"
   capture confirm file `filename'
   if _rc {
      copy "https://www.bls.gov/tus/special.requests/`filename'" "`filename'"
   }
}

// Eldercare
confirmdir "`ext_dir'/atusrostec_11`end_year'"
if r(confirmdir) == "170" {
   mkdir "`ext_dir'/atusrostec_11`end_year'"
}
cd "`ext_dir'/atusrostec_11`end_year'"
local filename "atusrostec_11`end_year'.zip"
di "`filename'"
capture confirm file `filename'
if _rc {
   copy "https://www.bls.gov/tus/special.requests/`filename'" "`filename'"
}

// Wellbeing
confirmdir "`ext_dir'/atuswb_`wb_start'`wb_end'"
if r(confirmdir) == "170" {
   mkdir "`ext_dir'/atuswb_`wb_start'`wb_end'"
}
cd "`ext_dir'/atuswb_`wb_start'`wb_end'"
foreach f in "resp" "act" "rwgts" "awgts" {
   local filename "wb`f'_`wb_start'`wb_end'.zip"
   capture confirm file `filename'
   if _rc {
      copy "https://www.bls.gov/tus/special.requests/`filename'" "`filename'"
   }
}

// Leave
confirmdir "`ext_dir'/atuslv"
if r(confirmdir) == "170" {
   mkdir "`ext_dir'/atuslv"
}
cd "`ext_dir'/atuslv"
local filename "lvresp_`lv_start'.zip"
capture confirm file `filename'
if _rc {
   copy "https://www.bls.gov/tus/special.requests/`filename'" "`filename'"
}
local filename "lvwgts_`lv_start'.zip"
capture confirm file `filename'
if _rc {
   copy "https://www.bls.gov/tus/special.requests/`filename'" "`filename'"
}

// Health and Eating
confirmdir "`ext_dir'/atuseh"
if r(confirmdir) == "170" {
   mkdir "`ext_dir'/atuseh"
}
cd "`ext_dir'/atuseh"
foreach eh_year of local health_years {
   foreach f in "resp" "child" "act" "wgts" {
      if "`eh_year'"!="2014" & "`f'" != "child" {
         local filename "eh`f'_`eh_year'.zip"
         capture confirm file
         if _rc {
            copy "https://www.bls.gov/tus/special.requests/`filename'" "`filename'"
         }
      }
   }
}

   
